/**
 * Created by root on 17-7-30.
 */
var ipListDao = require('../dao/ipListDao');
var stringUtils = require('../utils/stringUtils');
var logger = require('../config/log4js');

module.exports = {

    addIP : async function (IP, callback) {

        if (stringUtils.isEmpty(IP.ip, IP.port)) {
            logger.error.info("IP对象不完整! " + IP);
            return;
        }

        var ipResult = await ipListDao.queryIP(IP.ip, IP.port, function (result) {});
        logger.access.info("ipResult " + ipResult);

        if (ipResult.length == 0) {
            var ipAddress = global.ipnet.find(IP.ip);
            logger.access.info(ipAddress);
            var country = ipAddress[0];// 国家
            var province = ipAddress[1];// 省份
            var city = ipAddress[2];// 市
            IP.country = country;
            IP.province = province;
            IP.city = city;
            IP.status = 0;
            IP.createDate = new Date();
            var addResult = await ipListDao.addIP(IP, function (result) {});
            callback(addResult);
            return;
        }
        callback(ipResult);
    },

    getIPByStatus : async function (status, callback) {

        if (stringUtils.isEmpty(status)) {
            logger.error.info("状态不能为Null! " + status);
            return;
        }

        var ipResult = await ipListDao.queryIPByStatus(status, function (result) {});
        callback(ipResult);
    },

    updateByIPAndPort : async function (IP, callback) {

        if (stringUtils.isEmpty(IP.ip, IP.port, IP.status, IP.validateCount)) {
            logger.error.info("状态不能为Null! " + status);
            return;
        }

        var ipResult = await ipListDao.updateByIPAndPort(IP, function (result) {});
        callback(ipResult);
    },

    queryIPBySize : async function(size, callback) {
        var result = await ipListDao.queryIPBySize(size, function (result) {});
        callback(result);
        return result;
    }



}